package com.shengdong.water.feature.management.inspection


import android.util.Log
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.graphics.toColorInt
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.alibaba.android.arouter.facade.annotation.Route
import com.hjq.toast.Toaster
import com.kongzue.dialogx.dialogs.BottomDialog
import com.kongzue.dialogx.dialogs.WaitDialog
import com.kongzue.dialogx.interfaces.DialogLifecycleCallback
import com.kongzue.dialogx.interfaces.OnBindView
import com.shengdong.water.R
import com.shengdong.water.code.adapter.SiteInspectionAdapter
import com.shengdong.water.code.base.BaseRefreshActivity
import com.shengdong.water.code.bean.RealTimeBean
import com.shengdong.water.code.config.RouterConfig
import com.shengdong.water.code.router.RouterManager
import com.shengdong.water.code.viewmodel.InspectViewModel
import com.shengdong.water.code.viewmodel.PageListViewModel
import com.shengdong.water.code.widget.WheelView
import com.shengdong.water.databinding.ActivitySiteInspectionBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.util.Objects

@Route(path = RouterConfig.Management.INSPECT_PATH)
@AndroidEntryPoint
class SiteInspectionActivity : BaseRefreshActivity<RealTimeBean, ActivitySiteInspectionBinding, InspectViewModel>() {

    override fun getLayoutId() = R.layout.activity_site_inspection

    override fun getViewModelClass() = InspectViewModel::class.java

    override fun getRecyclerView() = binding.recyclerInspection

    override fun getSmartRefreshLayout() = binding.refresh

    override fun getAdapter() = SiteInspectionAdapter()

    private val outletList: MutableList<String> = mutableListOf()

    private val categoryList: MutableList<String> = mutableListOf()

    private var outletName = ""
    private var sewageOutlets = 0

    override fun showLoading(loading: String) {
        WaitDialog.show(loading)
    }

    override fun dismissLoading() {
        super.dismissLoading()
        WaitDialog.dismiss()
    }

    override fun initViews() {
        super.initViews()
        setTitle("现场监察")
        setupListener()
    }

    private fun setupListener() {

        binding.selectFilter.setOnClickListener { it ->
            when (it.id) {
                R.id.select_filter1 -> {
                    viewModel.outlets.value.map { it ->
                        outletList.add(it.name)
                    }
                    bottomOptionDialog(outletList, 0)
                }

                R.id.select_filter2 -> {
                    viewModel.category.value.map {
                        categoryList.add(it.dictValue)
                    }
                    bottomOptionDialog(categoryList,1)
                }
            }
        }

        baseRecyclerAdapter.setOnItemClickListener { item, pos ->
            RouterManager.build(RouterConfig.Management.MONITOR_DETAIL_PATH)
                .withString("id", item.id).navigate()
        }


    }




    override fun showError(message: String) {
        Toaster.show(message)
    }


    override fun initData() {
        super.initData()
        loadData()
        viewModel.getAllOutlets()
        viewModel.getOutletCategory()

    }

    override fun loadData() {
        val queryMap: MutableMap<String, Any> = mutableMapOf()
        queryMap["current"] = currentPage
        queryMap["size"] = pageSize
        viewModel.getRealTimeList(queryMap)
    }

    override fun observeData() {
        super.observeData()
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
                    viewModel.realTimeList.collect { pageData ->
                        pageData.let {
                            println(it.isEmpty())
                            if(currentPage == 1 && it.isEmpty()){
                                baseRecyclerAdapter.clearItems()
                                binding.recyclerInspection.setEmptyView(binding.layoutEmpty.root)
                                return@collect
                            }
                            if (it.isEmpty()) {
                                finishRefreshAndLoadMore(true, currentPage)
                                return@collect
                            }
                            handleData(it)
                        }

                    }
                }

            }

        }
    }


    private fun bottomOptionDialog(options: MutableList<String>, type : Int) {
        BottomDialog.build().setDialogLifecycleCallback(object :
            DialogLifecycleCallback<BottomDialog>() {
            override fun onShow(dialog: BottomDialog?) {
                super.onShow(dialog)
            }

            override fun onDismiss(dialog: BottomDialog?) {
                super.onDismiss(dialog)

            }
        }

        ).setCustomView(object : OnBindView<BottomDialog>(R.layout.dialog_outlet) {
            override fun onBind(dialog: BottomDialog, v: View) {
                val wheelView = v.findViewById<WheelView>(R.id.wheel_outlet)

                wheelView.setDataItems(options)

                wheelView.setOnItemSelectedListener(object : WheelView.OnItemSelectedListener {
                    override fun onItemSelected(
                        wheelView: WheelView,
                        data: Any,
                        position: Int
                    ) {
                        if(type == 0){
                            outletName = viewModel.outlets.value[position].name
                        }else {
                            sewageOutlets = viewModel.category.value[position].dictKey
                        }

                    }

                })

                v.findViewById<AppCompatTextView>(R.id.tv_confirm).setOnClickListener {
                    // 关闭对话框
                    dialog.dismiss()
                    val queryMap: MutableMap<String, Any> = mutableMapOf()
                    queryMap["current"] = currentPage
                    queryMap["size"] = pageSize
                    if(type == 0){
                        queryMap["name"] = outletName
                    }else {
                        queryMap["sewageOutletType"] = sewageOutlets
                    }
                    viewModel.getRealTimeList(queryMap)

                }

                val tvCancel = v.findViewById<AppCompatTextView>(R.id.tv_cancel)
                tvCancel.setOnClickListener {
                    dialog.dismiss()
                }
            }
        }).setMaskColor("#66000000".toColorInt()).setAllowInterceptTouch(false).show()
    }

}